<script lang="ts" setup name="SealCity">
import { ref } from 'vue';
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'
import { watch } from 'vue';


// 父向子传默认地址
withDefaults(defineProps<{
    userAddress?:string
}>(),{
    userAddress:""
})

// 选择地址 子向父传值
const emits = defineEmits<{
    // (e:"changeAddress",data:string):void
    changeAddress:[data:CityResult]
}>()


//1.点击配送地址设置背景颜色与省市区隐藏显示
const active = ref(false)
const target = ref(null)
onClickOutside(target,()=>{
    // console.log("点击了外面");
    active.value = false
    // cityList.value = cacheList.value
})

// 2.渲染弹层的省市区数据(ajax获取数据)
// 一次获取所有的省市区的数据 1次 *
// 先获取省份数据，点击省获取该省份的市数据，点击市获取区县数据 （3）
// 省市区的数据不会经常变动，会提前保存到文件中

type CityList = {
    name:string
    level:number
    code:string
    areaList:CityList[]
}

const cityList =ref<CityList[]>([]) 
const cacheList =ref<CityList[]>([]) 
const getCityList = async ()=>{
    const res = await axios.get('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
    // console.log(res);
    cityList.value = res.data
    cacheList.value =res.data
}
getCityList()

// 省市区选择
export type CityResult = {
  provinceCode: string
  provinceName: string
  cityCode: string
  cityName: string
  countyCode: string
  countyName: string
}


const changeResult = ref({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: ''
})
const selectCity = (city:CityList)=>{
    // console.log(city);
    if(city.level===0){
        changeResult.value.provinceName = city.name
        changeResult.value.provinceCode = city.code
        cityList.value = city.areaList
    }
    if(city.level===1){
        changeResult.value.cityName = city.name
        changeResult.value.cityCode = city.code
        cityList.value = city.areaList
    }
    if(city.level===2){
        changeResult.value.countyName = city.name
        changeResult.value.countyCode = city.code
        // cityList.value = city.areaList
        active.value = false
        // cityList.value = cacheList.value
        emits('changeAddress',changeResult.value)
    }
    
}
// 侦听器侦听active的变化：当active的值为false时，重置cityList的数据
watch(active,()=>{
    if(!active.value){
        cityList.value = cacheList.value
    }
})


</script>

<template>
  <div class="seal-city" ref="target">
    <div class="select" :class="{active:active}" @click="active=!active">
      <span class="value" v-if="userAddress">{{ userAddress }}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <span class="ellipsis" v-for="item in cityList" :key="item.code" @click="selectCity(item)">
        {{ item.name }}
      </span>
    </div>
  </div>
</template>

<style scoped lang="less">
.seal-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
